package com.xjh.controller;

import com.xjh.bo.*;
import com.xjh.jparepository.AccountRepository;
import com.xjh.jparepository.AclUserRepository;
import com.xjh.jparepository.JpaStudentRepository;
import com.xjh.po.*;
import com.xjh.utils.resultutils.ResponseObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/jpa")
public class TestJPAController {
    @Autowired
    private AclUserRepository aclUserRepository;
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private JpaStudentRepository jpaStudentRepository;

    // 使用自带方法查询
    @GetMapping("/getUser")
    public ResponseObj get() {
        List<AclUserPO> all = aclUserRepository.findAll();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(all);
    }


    // 自定义方法查询
    @GetMapping("/findAllByUsername")
    public ResponseObj findAllByUsername() {
        List<AclUserPO> all = aclUserRepository.findAllByUsername("xjh");
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(all);
    }

    // 2.使用JPQL的方式查询:注意参数为实体类和属性，与表和字段无关
    @GetMapping("/findUsers")
    public ResponseObj findUsers() {
        List<AclUserPO> all = aclUserRepository.findUsers();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(all);
    }

    // 3. 使用原生sql查询
    @GetMapping("/findAllByUsernameNativeQuery")
    public ResponseObj findAllByUsernameNativeQuery() {
        int xjh = aclUserRepository.findAllByUsernameNativeQuery("xjh");
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(xjh);
    }

    // 多表操作==1对1插入,添加账户，同时添加账户所属学生
    @GetMapping("/tesetOneToOneInsert")
    public ResponseObj tesetOneToOneInsert() {

        JpaStudent jpaStudent = new JpaStudent();
        jpaStudent.setId(3232L);
        jpaStudent.setStuName("teset");
        JpaAccount jpaAccount = new JpaAccount();
        jpaAccount.setId(34242L);
        jpaAccount.setMoney(new BigDecimal("999"));
        jpaAccount.setJpaStudent(jpaStudent);
        JpaAccount save = accountRepository.save(jpaAccount);
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(save);
    }
    // 多表操作==1对1获取,获取账户，同时获取账户所属学生
    @GetMapping("/testOneToOneSelect")
    public ResponseObj testOneToOneSelect() {
        List<JpaAccount> all = accountRepository.findAll();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(all);
    }

    // 多表操作==1对1删除,删除账户，同时删除账户所属学生
    @GetMapping("/testOneToOneDelete")
    public ResponseObj testOneToOneDelete() {


      accountRepository.deleteById(34242L);
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok();
    }
    // 多表操作==1对多查询,查询学生，同时查询学生的所有账户
    @GetMapping("/testOneToMoneySelect")
    public ResponseObj testOneToMoneySelect() {
        List<JpaStudent> all = jpaStudentRepository.findAll();
        ResponseObj responseObj = new ResponseObj();
        return responseObj.ok(all);
    }

}
